/*
    --------------------------------------------------------------------------
    Code for link-hover text boxes
    By Nicolas Hoening (Web Site: http://nicolashoening.de)
    usage: <a onmouseover="popup('popup content', width)">a link</a>
     (width is optional - default is in CSS: #pup {width: x;},
      escape " in content with &quot;)
    --------------------------------------------------------------------------
*/

// create the popup box - remember to give it some width in your styling 
document.write('<div id="pup" style="position:absolute; display:none; z-index:200;">sadasdasd</div>');

var minMargin = 15; // set how much minimal space there should be to
                            // between the popup and everything else (borders, mouse)
var ready = false; // we are ready when the mouse event is set up
var default_width = '200px'; // will be set to width from css in document.ready

function getPup() {
	return document.getElementById('pup');
}

jQuery(document).ready(function(){
	var pup = getPup();
	pup.hide();
    default_width = pup.style.width;
    
    // set dynamic coords when the mouse moves
    document.onmousemove = function(e){ 
        var x,y;
      
        x = document.body.scrollLeft + e.clientX;
        y = document.body.scrollTop + e.clientY;

        x += 10; // important: if the popup is where the mouse is, the hoverOver/hoverOut events flicker
      
        var x_y = nudge(x,y); // avoids edge overflow
      
        var pup = getPup();
        // remember: the popup is still hidden
        pup.style.top = x_y[1] + 'px';
        pup.style.left = x_y[0] + 'px';
    };

    ready = true;
});

// avoid edge overflow
function nudge(x,y)
{
    var pup = getPup();
    
    // When the mouse is too far on the right, put window to the left
    var xtreme = document.body.scrollLeft + document.body.width - pup.getWidth() - minMargin;
    if(x > xtreme) {
        x -= pup.getWidth() + 2 * minMargin;
    }
    x = max(x, 0);

    // When the mouse is too far down, move window up
    if((y + pup.getHeight()) > (document.body.height + document.body.scrollTop)) {
        y -= pup.getHeight() + minMargin;
    }

    return [ x, y ];
}

function popup(msg, width)
{
	var pup = getPup();
	
	if (typeof width === "undefined"){
        width = default_width;
    }
    
    // write content and display
    if (ready) {
    	pup.style.width = width + "px";
    	$("pup").update(msg);
    	pup.show();
    }
    // make sure popup goes away on mouse out
    this.onmouseout = function(e){
        var pup = getPup();
    	
    	pup.hide();
        pup.style.width = default_width;
    };
}


function max(a,b){
    if (a>b) return a;
    else return b;
}


